package com.example.administrator.capital.mine.second;


import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.capital.R;
import com.example.administrator.capital.base.BaseBackFragment;
import com.example.administrator.capital.eventbus.EventBase;
import com.example.administrator.capital.presenter.MinePresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.CommonUtils;
import com.example.administrator.capital.utils.PreferenceManager;
import com.example.administrator.capital.utils.ToastUtil;
import com.example.administrator.capital.widget.CountDownTimerUtils;
import com.lljjcoder.citypickerview.widget.CityPicker;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import eventbusactivityscope.EventBusActivityScope;

/**
 * A simple {@link Fragment} subclass.
 * 绑定银行卡
 */
public class BindCardFragment extends BaseBackFragment {

    @BindView(R.id.et_card_name)
    EditText mEtCardName;
    @BindView(R.id.et_card_number)
    EditText mEtCardNumber;
    @BindView(R.id.rl_bank)
    RelativeLayout mRlBank;
    @BindView(R.id.rl_city)
    RelativeLayout mRlCity;
    @BindView(R.id.et_bank_number)
    EditText mEtBankNumber;
    @BindView(R.id.et_card_phone)
    EditText mEtCardPhone;
    @BindView(R.id.tv_send_code)
    TextView mTvSendCode;
    @BindView(R.id.et_card_code)
    EditText mEtCardCode;
    @BindView(R.id.btn_bind)
    Button mBtnBind;
    @BindView(R.id.scroll_view)
    NestedScrollView mScrollView;
    Unbinder unbinder;
    @BindView(R.id.city)
    TextView mCity;
    @BindView(R.id.et_openbank)
    EditText etOpenbank;
    private TextView mBack;
    private String phone;
    private MinePresenter minePresenter;

    public BindCardFragment() {
        // Required empty public constructor
    }

    public static BindCardFragment newInstance() {

        Bundle args = new Bundle();
        BindCardFragment fragment = new BindCardFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_bind_card, container, false);
        unbinder = ButterKnife.bind(this, view);
        minePresenter = new MinePresenter(null);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mBack = view.findViewById(R.id.app_title_back);
    }

    @Override
    protected void setListener() {
        super.setListener();
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mActivity.onBackPressed();

            }
        });
        mTvSendCode.setOnClickListener(this);
        mRlBank.setOnClickListener(this);
        mRlCity.setOnClickListener(this);
        mBtnBind.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_send_code:
                if (isPhoneNumber()) {
                    CountDownTimerUtils utils = new CountDownTimerUtils(mTvSendCode, 60000, 1000);
                    utils.start();
                }
                break;
            case R.id.rl_bank:
                break;
            case R.id.rl_city:
                selectAddress();
                break;
            case R.id.btn_bind:
                if (isPhoneNumber()) {
                    addCard();
                }

                break;
        }
    }
    private String province;
    private String city;
    private String district;
    private void selectAddress() {
        CityPicker cityPicker = new CityPicker.Builder(getActivity())
                .textSize(14)
                .title("地址选择")
                .titleBackgroundColor("#FFFFFF")
                .confirTextColor("#0071a9")
                .cancelTextColor("#0071a9")
                .province("河南省")
                .city("郑州市")
                .district("金水区")
                .textColor(Color.parseColor("#000000"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .itemPadding(15)
                .onlyShowProvinceAndCity(false)
                .build();
        cityPicker.show();
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... citySelected) {
                province = citySelected[0];
                city = citySelected[1];
                district = citySelected[2];
                mCity.setText(province + "-" + city + "-" + district);
            }
        });
    }

    @Override
    protected void setData() {
        super.setData();
    }

    private void addCard() {
        minePresenter.addbankcarduser(new IViewRequest<String>() {
                                          @Override
                                          public void onFailMsg(String var) {
                                              ToastUtil.showShort(var);
                                          }
                                          @Override
                                          public void onCode(int code) {
                                              ToastUtil.showShort(code);
                                          }
                                          @Override
                                          public void onSuccess(Response<String> data) {
                                              ToastUtil.showShort("绑定成功");
                                              _mActivity.onBackPressed();
                                          }
                                          @Override
                                          public void onFail(Response<String> error) {
                                          }
                                      }, PreferenceManager.instance().getUseId(),
                etOpenbank.getText().toString().trim(),
                "",
                mEtBankNumber.getText().toString().trim(),
                mEtCardName.getText().toString(),
                mCity.getText().toString(),
                mEtCardNumber.getText().toString(),""
        );
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private boolean isPhoneNumber() {
        if (TextUtils.isEmpty( mEtCardName.getText().toString())) {
            Toast.makeText(getContext(), "姓名不能为空!", Toast.LENGTH_SHORT).show();
            return false;
        } else if (TextUtils.isEmpty( mEtCardNumber.getText().toString())) {
            Toast.makeText(getContext(), "身份证不能为空!", Toast.LENGTH_SHORT).show();
            return false;
        } else if (TextUtils.isEmpty( etOpenbank.getText().toString())) {
            Toast.makeText(getContext(), "所属银行不能为空", Toast.LENGTH_SHORT).show();
            return false;
        } else if (TextUtils.isEmpty( mCity.getText().toString())) {
            Toast.makeText(getContext(), "开户城市不能为空", Toast.LENGTH_SHORT).show();
            return false;
        } else if (TextUtils.isEmpty( mEtBankNumber.getText().toString())) {
            Toast.makeText(getContext(), "开户账号不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
